package com.ruoyi.yijing.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.yijing.mapper.YjReferencesMapper;
import com.ruoyi.yijing.domain.YjReferences;
import com.ruoyi.yijing.service.IYjReferencesService;

import static com.ruoyi.common.constant.CacheConstants.CACHE_PRE;

/**
 * 参考资料Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-22
 */
@Service
public class YjReferencesServiceImpl implements IYjReferencesService 
{
    @Autowired
    private YjReferencesMapper yjReferencesMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询参考资料
     * 
     * @param refId 参考资料主键
     * @return 参考资料
     */
    @Override
    public YjReferences selectYjReferencesByRefId(Long refId)
    {
        return yjReferencesMapper.selectYjReferencesByRefId(refId);
    }

    /**
     * 查询参考资料列表
     * 
     * @param yjReferences 参考资料
     * @return 参考资料
     */
    @Override
    public List<YjReferences> selectYjReferencesList(YjReferences yjReferences)
    {
        String key = CACHE_PRE +"selectYjReferencesList";
        List<YjReferences> rst = redisCache.getCacheList(key);
        if (rst == null || rst.size() == 0){
            rst = yjReferencesMapper.selectYjReferencesList(yjReferences);
            if (rst.size()>0){
                redisCache.setCacheList(key,rst);
                redisCache.expire(key,1, TimeUnit.MINUTES);
            }
        }
        return rst;
    }

    /**
     * 新增参考资料
     * 
     * @param yjReferences 参考资料
     * @return 结果
     */
    @Override
    public int insertYjReferences(YjReferences yjReferences)
    {
        yjReferences.setCreateBy(SecurityUtils.getUsername());
        yjReferences.setCreateTime(new Date());
        return yjReferencesMapper.insertYjReferences(yjReferences);
    }

    /**
     * 修改参考资料
     * 
     * @param yjReferences 参考资料
     * @return 结果
     */
    @Override
    public int updateYjReferences(YjReferences yjReferences)
    {
        yjReferences.setUpdateBy(SecurityUtils.getUsername());
        yjReferences.setUpdateTime(new Date());
        return yjReferencesMapper.updateYjReferences(yjReferences);
    }

    /**
     * 批量删除参考资料
     * 
     * @param refIds 需要删除的参考资料主键
     * @return 结果
     */
    @Override
    public int deleteYjReferencesByRefIds(Long[] refIds)
    {
        return yjReferencesMapper.deleteYjReferencesByRefIds(refIds);
    }

    /**
     * 删除参考资料信息
     * 
     * @param refId 参考资料主键
     * @return 结果
     */
    @Override
    public int deleteYjReferencesByRefId(Long refId)
    {
        return yjReferencesMapper.deleteYjReferencesByRefId(refId);
    }
}
